<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "transfer".
 *
 * @property integer $id
 * @property integer $income_user_id
 * @property integer $transfer_user_id
 * @property double $num
 * @property string $reason
 * @property string $transfer_time
 * @property string $create_at
 * @property string $update_at
 *
 * @property User $incomeUser
 * @property User $transferUser
 */
class Transfer extends \app\models\BaseModel {
    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'transfer';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['income_user_id', 'transfer_user_id', 'num', 'reason', 'transfer_time'], 'required'],
            [['income_user_id', 'transfer_user_id'], 'integer'],
            [['num'], 'number'],
            [['transfer_time', 'create_at', 'update_at'], 'safe'],
            [['reason'], 'string', 'max' => 255],
            [['income_user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['income_user_id' => 'id']],
            [['transfer_user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['transfer_user_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'income_user_id' => 'Income User ID',
            'transfer_user_id' => 'Transfer User ID',
            'num' => '金额',
            'reason' => '转账原因',
            'transfer_time' => 'Transfer Time',
            'create_at' => '创建时间',
            'update_at' => '更新时间',
        ];
    }

    public function fields() {
        $fields = [
            'id',
            'incomeUser',
            'transferUser',
            'num',
            'reason',
            'transfer_time',
        ];
        $fields = array_combine($fields, $fields);

        return $fields;
    }

    public function extraFields() {
        $fields = [
            'income_user_id',
            'transfer_user_id',
            'create_at',
            'update_at',
        ];
        $fields = array_combine($fields, $fields);
        return $fields;
    }

    public function getIncomeUser() {
        return $this->hasOne(User::className(), ['id' => 'income_user_id'])->inverseOf('transfers');
    }

    public function getTransferUser() {
        return $this->hasOne(User::className(), ['id' => 'transfer_user_id'])->inverseOf('transfers0');
    }
}
